/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: app2022 - T14
 * <p>Powered by webrx On 2022-02-07 11:01:57
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T14 {

    CyclicBarrier c = new CyclicBarrier(3, () -> {
        System.out.println("到起了，开饭了...%n");
    });

    public static void main(String[] args) {
        var t = new T14();
        new Thread(() -> t.eat(15), "李四").start();
        new Thread(() -> t.eat(2), "张三").start();
        new Thread(() -> t.eat(25), "赵强").start();
    }


    void eat(int s) {
        String t = Thread.currentThread().getName();
        System.out.printf("%s,开始动身，需要%d秒到达.%n", t, s);
        try {
            TimeUnit.SECONDS.sleep(s);
            System.out.printf("%s,已到达.%n", t);
            c.await();
            //c.await(15, TimeUnit.SECONDS);
            //System.out.println(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
